﻿using System;
using System.Data;
using Besture.Bll;
using Besture.Common.Helper;

namespace Besture.UI.Control
{
    public partial class SelectCity : System.Web.UI.UserControl
    {
        public bool required { get; set; }

        public long _ProvinceID;
        /// <summary>
        /// 省编号
        /// </summary>
        public long ProvinceID
        {
            get { return string.IsNullOrEmpty(ddlProvince.SelectedValue) ? 0 : Convert.ToInt32(ddlProvince.SelectedValue); }
            set { _ProvinceID = ProvinceID; }
        }

        public string _ProvinceName;
        /// <summary>
        /// 省名称
        /// </summary>
        public string ProvinceName
        {
            get { return string.IsNullOrEmpty(ddlProvince.SelectedItem.Text) ? "" : ddlProvince.SelectedItem.Text; }
            set { _ProvinceName = ProvinceName; }
        }

        public long _CityID;
        /// <summary>
        /// 市编号
        /// </summary>
        public long CityID
        {
            get { return  string.IsNullOrEmpty(ddlCity.SelectedValue) ? 0 : Convert.ToInt32(ddlCity.SelectedValue); }
            set { _CityID = CityID; }
        }
        public string _CityName;
        /// <summary>
        /// 城市名称
        /// </summary>
        public string CityName
        {
            get { return string.IsNullOrEmpty(ddlCity.SelectedItem.Text) ? "" : ddlCity.SelectedItem.Text; }
            set { _CityName = CityName; }
        }

        public long _DistrictID; 

        public string _DistrictName; 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定省份数据
                DataTable tableProvince = QueryDac.GetTable("SELECT * FROM TBProvince");
                ControlHelper.BindDropDownList(ddlProvince, tableProvince, "ProName", "ProID");
                ControlHelper.InitDropDownList(ddlCity, null); 
                if (required)
                {
                    ddlCity.CssClass = "cityRequired";
                }

                //初始化
                if (_ProvinceID > 0)
                {
                    ddlProvince.SelectedValue = _ProvinceID.ToString();
                    //加载市数据
                    DataTable tableCity = QueryDac.GetTable("SELECT * FROM TBCity  WHERE ProID="+_ProvinceID);
                    ControlHelper.BindDropDownList(ddlCity, tableCity, "CityName", "CityID");
                }
                if (_CityID > 0)
                {
                    ddlCity.SelectedValue = _CityID.ToString();
                    
                } 
            }

        }

        /// <summary>
        /// 选择省
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取选择省
            if (string.IsNullOrEmpty(ddlProvince.SelectedValue) || ddlProvince.SelectedValue.Equals("0"))
            {
                ControlHelper.InitDropDownList(ddlCity, null);
                //ControlHelper.InitDropDownList(ddlDistrict, null);
                return;
            }
            int proID = Convert.ToInt32(ddlProvince.SelectedValue);
            DataTable tableCity = QueryDac.GetTable("SELECT * FROM TBCity  WHERE proID=" + proID);
            ControlHelper.BindDropDownList(ddlCity, tableCity, "CityName", "CityID"); 
        }

        /// <summary>
        /// 选择市
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取选择市
            if (string.IsNullOrEmpty(ddlCity.SelectedValue) || ddlCity.SelectedValue.Equals("0"))
            { 
                return;
            }
            int cityID = Convert.ToInt32(ddlCity.SelectedValue); 
        }

        /// <summary>
        /// 重新加载
        /// </summary>
        public void PostBack()
        {
            //初始化
            if (_ProvinceID > 0)
            {
                ddlProvince.SelectedValue = _ProvinceID.ToString();
                //加载市数据
                DataTable tableCity = QueryDac.GetTable("SELECT * FROM TBCity  WHERE proID=" + _ProvinceID);
                ControlHelper.BindDropDownList(ddlCity, tableCity, "CityName", "CityID"); 
            }
            if (_CityID > 0)
            {
                ddlCity.SelectedValue = _CityID.ToString(); 
            } 

        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        public  void InitData()
        {
            DataTable tableProvince = QueryDac.GetTable("SELECT * FROM TBProvince");
            ControlHelper.BindDropDownList(ddlProvince, tableProvince, "ProvinceName", "ProvinceID"); 
            ControlHelper.InitDropDownList(ddlCity, null); 
            if (required)
            {
                ddlCity.CssClass = "cityRequired";
            }
        }
    }
}